package manager;

import entity.Book;
import entity.Category;
import iface.IManager;
import java.util.ArrayList;

/**
 *
 * @author User
 */
public class BookManager implements IManager<Book> {

    private Manager manager;
    private ArrayList<Book> bookList;

    public BookManager(Manager manager) {
        this.manager = manager;
        bookList = new ArrayList<Book>();
    }
    
    public ArrayList<Book> getAllElements() {
        boolean flag;
        ArrayList<Book> alBook = new ArrayList<Book>();
        for(Book book: this.bookList) {
            flag = false;
            for(Book book1: alBook) {
                if(book1.getTitle().equals(book.getTitle())) {
                    flag = true;
                    break;
                }
            }
            if(flag == false) alBook.add(book);
        }
        return alBook;
    }

    @Override
    public void add(Book obj) {
        this.bookList.add(obj);
    }

    @Override
    public void remove(Book obj) {
        this.bookList.remove(obj);
        obj.setStatus(false);
        this.manager.getDisabledBookManager().add(obj);
    }

    @Override
    public Book replace(int index, Book obj) {
        return this.bookList.set(index, obj);
    }

    @Override
    public int size() {
        return this.bookList.size();
    }

    @Override
    public Book get(int index) {
        return this.bookList.get(index);
    }
    
    public Book getByBookID(int bookID) {
        for(Book book: this.bookList) {
            if(book.getBookID() == bookID) return book;
        }
        return null;
    }
    
    public ArrayList<Book> getByBookName(String bookName) {
        ArrayList<Book> alBook = new ArrayList<Book>();
        for(Book book: this.bookList) {
            if(book.getTitle().equals(bookName))
                alBook.add(book);
        }
        return alBook;
    }

    public ArrayList<Book> getByCategoryID(Category category) {
        ArrayList<Book> alBook = new ArrayList<Book>();
        for(Book book: this.bookList) {
            if(book.getCategoryID() == category.getCategoryID())
                alBook.add(book);
        }
        return alBook;
    }
}
